package nacos

import (
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
)

type Config struct {
	Server           string `yaml:"server"`
	Port             uint64 `yaml:"port"`
	NamespaceId      string `yaml:"namespaceId"`
	PrivateHotConfig string `yaml:"privateHotConfig"`
	UniteHotConfig   string `yaml:"uniteHotConfig"`
	CustomizedConfig string `yaml:"customizedConfig"`
	CommonConfig     string `yaml:"commonConfig"`
	Group            string `yaml:"group"`
	LogLevel         string `yaml:"logLevel"`
}

type HotConfig struct {
	Backend  *bkConfig `yaml:"backend"`
	Consumer *csConfig `yaml:"consumer"`
	Producer *pdConfig `yaml:"producer"`
}

type bkConfig struct {
}

type csConfig struct {
	IsAlarm bool `yaml:"isAlarm"`
}

type pdConfig struct {
	MintInterval *uint `yaml:"mintInterval"`
	LoanSwitch   bool  `yaml:"loanSwitch"`
}

func InitClient(c *Config) (config_client.IConfigClient, error) {
	//create ServerConfig
	sc := []constant.ServerConfig{
		*constant.NewServerConfig(c.Server, c.Port, constant.WithContextPath("/nacos")),
	}

	//create ClientConfig
	cc := *constant.NewClientConfig(
		constant.WithNamespaceId(c.NamespaceId),
		constant.WithTimeoutMs(5000),
		constant.WithNotLoadCacheAtStart(true),
		constant.WithLogDir("./nacos-log"),
		constant.WithCacheDir("./nacos-cache"),
		constant.WithLogLevel(c.LogLevel),
		constant.WithUsername("nacos"),
		constant.WithPassword("nacos"),
	)

	// create config client
	client, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		},
	)

	return client, err
}
